package com.android.boncen.commonFrame.fragment.recommend_framents;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.boncen.commonFrame.R;
import com.android.boncen.commonFrame.activity.Webview_detail_activity;
import com.android.boncen.commonFrame.adapter.MyRecyclerViewAdapter;

/**
 * Created by Boncen on 2017/1/24.
 */

public class recommend_eat_fragment extends Fragment {
    private String title;
    private View rootView;
    private Context mContext;
    private RecyclerView recyclerView_goodtoeat;
    private RecyclerView recyclerView_badtoeat;
    private RecyclerView recyclerView_cookbook;
    MyRecyclerViewAdapter goodFood_adapter;
    MyRecyclerViewAdapter forbiddenFood_adapter;
    MyRecyclerViewAdapter cookbook_adapter;

    private View popContentView;
    private TextView popup_food_title;
    private TextView popup_food_decription;
    private ImageView popup_food_img;

   private String[] goodFoods = {"荞麦","玉米","燕麦","小米","红薯","魔芋","苦瓜","生菜"};
    private String[] forbiddenFoods={"糯米","葡萄","红枣","芋头","菠萝","枇杷","板栗","葡萄干"};
    private String[] cookbooks={"无糖荞麦桃酥","酸辣荞麦面","玉米面萝卜煎饺","全麦玉米粒面包","燕麦饼卷土豆丝","燕麦苦瓜酿","糙米燕麦南瓜粥","凉拌绿豆芽",
    "香辣肉片魔芋","葱烧海参","芦笋炒鸡片","清炒芦笋灯笼椒","芦笋炒虾仁","蒜臼仔姜茄子","蒸茄子"};
    private String[] goodFood_descriptions = {"荞麦富含碳水化合物，蛋白质和膳食纤维，并含有丰富的B族维生素，有降低血脂，保护视力，软化血管的作用，是糖尿病人的理想食材。",
            "玉米素有长寿食品的美称，其含有丰富的蛋白质、脂肪、维生素、微量元素、纤维素及多糖等，能够降血糖，降低血液黏稠度。",
            "燕麦含有丰富的膳食纤维及B族维生素，可以降低血压、降低胆固醇、防治大肠癌、防治心脏疾病，对控制餐后血糖效果十分显著。",
            "小米中含有的B族维生素的量居所有粮食之首，不仅可以健脾养胃，还可以改善糖尿病患者的心肌功能和血液循环。",
            "红薯中含有丰富的膳食纤维，对促进肠道蠕动和防止便秘非常有益，还可以延缓糖尿病人餐后血糖的升高。红薯中的硒元素，可以起到辅助调节血糖的作用。",
            "魔芋低热量、低脂、低糖，含有十六种氨基酸，十种矿物质微量元素和丰富的食物纤维，可以延缓葡萄糖的吸收，降低餐后血糖，减轻胰腺负担，对糖尿病人十分有益。",
            "苦瓜的维生素C含量很高，具有预防坏血病、保护细胞膜、防止动脉粥样硬化、提高机体应激能力、保护心脏等作用，还可以降糖消脂，刺激体内胰岛素分泌，十分适合糖尿病人。",
            "生菜含有丰富的膳食纤维以及维生素C，对调节血糖、血脂，预防动脉硬化都有很好的作用，同时也是十分适宜生吃的食材，十分适宜糖尿病人。"};
    private String[] forbiddenFoods_descriptions = {
            "糯米虽然含有丰富的淀粉与B族维生素，对于普通人来说，可以健脾养胃，补中益气。但是对于糖尿病人来说，它所含糖分较多，升糖指数也比较高，不利于控制血糖。",
            "葡萄口味酸甜，富含花青素，一直是众人特别是女性抗衰老的首选水果，但是它含有较多糖分，并且很大部分都是葡萄糖，十分易于被人体吸收，不易于糖尿病人控制血糖。",
            "红枣虽然一直有天然维生素丸的美誉，但是因为其含糖量非常高，十分不易于糖尿病人控制血糖，所以糖尿病人一般不推荐食用。",
            "芋头口感细软，绵甜香糯，富含淀粉，十分易于被人体吸收，对于糖尿病人来说，芋头的升糖指数较高，对于控制血糖十分不利，所以糖尿病人尽量不要食用。",
            "菠萝是夏初药食兼用的水果，不过因其含有大量的葡萄糖与和果糖，升糖指数也较高，对于需要控制血糖的糖尿病人来说，不适宜食用。",
            "枇杷含有丰富的糖类，是有名的润肺止咳的食材，但是对于糖尿病人来说，糖分含量太高并且多半都是葡萄糖，易被吸收，十分不易于控制血糖，所以需要少食用。",
            "板栗含有丰富的淀粉及糖类，虽然升糖指数比米饭要低，但是糖分含量比较高，所以糖尿病人食用时也要慎重。",
            "葡萄干的糖分含量较葡萄更高，并且大部分都是葡萄糖，十分容易被人体吸收，是糖尿病人需要禁止食用的食材。"
    };

    private String[] urls = {"http://www.haodou.com/recipe/221696", "http://www.haodou.com/recipe/311789","http://www.haodou.com/recipe/290124/",
            "http://www.haodou.com/recipe/274928","http://www.haodou.com/recipe/304515/","http://www.haodou.com/recipe/219117/",
            "http://www.haodou.com/recipe/265265/","http://www.haodou.com/recipe/309614/","http://www.haodou.com/recipe/294031/",
            "http://www.haodou.com/recipe/252932/","http://www.haodou.com/recipe/300044/","http://www.haodou.com/recipe/288897/",
            "http://www.haodou.com/recipe/285064/","http://www.haodou.com/recipe/310551","http://www.haodou.com/recipe/298327"};

    private int[] goodFoods_imgs ={R.drawable.food_1,R.drawable.food_2,R.drawable.food_3,R.drawable.food_4,R.drawable.food_5,
            R.drawable.food_6,R.drawable.food_7,R.drawable.food_8};
    private int[] forbiddenFoods_imgs ={R.drawable.food_9,R.drawable.food_10,R.drawable.food_11,R.drawable.food_12,R.drawable.food_13,
            R.drawable.food_14,R.drawable.food_15,R.drawable.food_16};
    private int[] cookbook_imgs = {R.drawable.cookbook_1,R.drawable.cookbook_2,R.drawable.cookbook_3,R.drawable.cookbook_4,R.drawable.cookbook_5,
            R.drawable.cookbook_6,R.drawable.cookbook_7,R.drawable.cookbook_8,R.drawable.cookbook_9,R.drawable.cookbook_10,R.drawable.cookbook_11,
            R.drawable.cookbook_12,R.drawable.cookbook_13,R.drawable.cookbook_14,R.drawable.cookbook_15,};

    public recommend_eat_fragment(){}
    public recommend_eat_fragment(Context context,String title){
        this.mContext = context;
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //初始化饮食推荐页面上的popup窗口
        popContentView = View.inflate(mContext,R.layout.popupwindow_food,null);
        popup_food_title = (TextView) popContentView.findViewById(R.id.popup_food_title);
        popup_food_decription = (TextView) popContentView.findViewById(R.id.popup_food_decription);
        popup_food_img = (ImageView) popContentView.findViewById(R.id.popup_food_img);

    }

    @Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView==null){
            rootView = View.inflate(mContext, R.layout.eat_view,null);
        }
        recyclerView_goodtoeat = (RecyclerView) rootView.findViewById(R.id.recyclerview_good_to_eat);
        recyclerView_badtoeat = (RecyclerView) rootView.findViewById(R.id.recyclerview_bad_to_eat);
        recyclerView_cookbook = (RecyclerView) rootView.findViewById(R.id.recyclerview_cookbook);


        goodFood_adapter = new MyRecyclerViewAdapter(mContext,goodFoods,goodFoods_imgs);
        forbiddenFood_adapter = new MyRecyclerViewAdapter(mContext,forbiddenFoods,forbiddenFoods_imgs);
        cookbook_adapter = new MyRecyclerViewAdapter(mContext,cookbooks,cookbook_imgs);

        goodFood_adapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                popup(position,goodFoods,goodFoods_imgs,goodFood_descriptions);
            }
        });
        forbiddenFood_adapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                popup(position,forbiddenFoods,forbiddenFoods_imgs,forbiddenFoods_descriptions);
            }
        });
        cookbook_adapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                //使用网页的方式浏览
                Intent intent = new Intent(mContext, Webview_detail_activity.class);
                intent.putExtra("title",cookbooks[position]);
                intent.putExtra("url",urls[position]);
                mContext.startActivity(intent);

            }
        });
        recyclerView_goodtoeat.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.HORIZONTAL));
        recyclerView_badtoeat.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.HORIZONTAL));
        recyclerView_cookbook.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.HORIZONTAL));

        recyclerView_goodtoeat.setAdapter(goodFood_adapter);
        recyclerView_badtoeat.setAdapter(forbiddenFood_adapter);
        recyclerView_cookbook.setAdapter(cookbook_adapter);
        return  rootView;
    }

    private void popup(int position,String[] title,int[] img,String[] decription) {
        PopupWindow popupWindow = new PopupWindow();
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        popup_food_title.setText(title[position]);
        popup_food_img.setImageResource(img[position]);
        popup_food_decription.setText(decription[position]);
        popupWindow.setContentView(popContentView);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0xffffff));
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        backgroundAlpha(0.5f);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1.0f);
            }
        });
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);
        popupWindow.showAtLocation(rootView, Gravity.CENTER,0,0);
    }

    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getActivity().getWindow().setAttributes(lp);
    }
}
